
package com.zheng.oa.admin.controller.wc;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zheng.common.base.BaseController;
import com.zheng.common.util.Pagination;
import com.zheng.common.util.StringUtil;
import com.zheng.hr.rpc.api.HrDictService;
import com.zheng.oa.common.constant.OaResult;
import com.zheng.oa.common.constant.OaResultConstant;
import com.zheng.oa.common.constant.enums.OaStatus;
import com.zheng.upms.dao.model.UpmsUser;
import com.zheng.upms.rpc.api.UpmsApiService;
import com.zheng.wc.dao.model.WcNotice;
import com.zheng.wc.dao.model.WcNoticeUser;
import com.zheng.wc.rpc.api.WcNoticeService;
import com.zheng.wc.rpc.api.WcNoticeUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2018/3/30
 * Time: 9:28
 */
@Controller
@Api(value = "通知管理", description = "通知管理")
@RequestMapping("/oa/wc/noticeUser")
public class WcNoticeUserController extends BaseController {
    @Autowired
    private WcNoticeUserService noticeUserService;
    @Autowired
    private UpmsApiService apiService;
    @Autowired
    private HrDictService dictService;

    @ApiOperation(value = "用户通知管理")
    @RequiresPermissions("oa:wc:noticeUser:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        return "/manage/wc/noticeUser/index.jsp";
    }

    @ApiOperation(value = "用户通知管理列表")
    @RequiresPermissions("oa:wc:noticeUser:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(@RequestParam(required = false, defaultValue = "1", value = "pageNumber") int pageNumber,
                       @RequestParam(required = false, defaultValue = "10", value = "pageSize") int pageSize,
                       @RequestParam(required = false, value = "sortName") String sortName,
                       @RequestParam(required = false, value = "sortOrder") String sortOrder,
                       WcNotice notice) {
        notice.getSqlMap().put("sql", apiService.sqlDataScopeFilter(getUsername(), "wn", "uu"));
        notice.setPagination(new Pagination(pageNumber, pageSize, sortOrder, sortName));
        notice.setStatus(OaStatus.NORMAL.getKey());
        String dictValue = dictService.getDictValue("已发布", "notice_state", "1");
        notice.setState(Integer.parseInt(dictValue));
        UpmsUser upmsUser = apiService.selectUpmsUserByUsername(getUsername());
        List<Map<String, Object>> rows = noticeUserService.selectUserNoticePages(notice, upmsUser.getUserId());
        Integer total = noticeUserService.selectUserNoticePagesCount(notice, upmsUser.getUserId());
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "查看用户通知")
    @RequiresPermissions("oa:wc:noticeUser:read")
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view(Model model, Integer id) {
        Map<String, Object> notice = noticeUserService.selectUserNotice(id);
        model.addAttribute("notice", notice);
        JSONArray files = JSONObject.parseArray(notice.get("files").toString());
        model.addAttribute("files", files);
        WcNoticeUser noticeUser = noticeUserService.selectById(id);
        noticeUser.setReadFlag(true);
        noticeUserService.updateById(noticeUser);
        return "/manage/wc/noticeUser/view.jsp";
    }

    @ApiOperation(value = "标记已读")
    @RequiresPermissions("oa:wc:noticeUser:read")
    @RequestMapping(value = "/read", method = RequestMethod.GET)
    @ResponseBody
    public Object read(Integer id) {
        WcNoticeUser noticeUser = noticeUserService.selectById(id);
        noticeUser.setReadFlag(true);
        if (noticeUserService.updateById(noticeUser)) {
            return new OaResult(OaResultConstant.SUCCESS, null);
        }
        return new OaResult(OaResultConstant.FAILED, null);
    }

    @ApiOperation(value = "删除用户通知管理")
    @RequiresPermissions("oa:wc:noticeUser:delete")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        try {
            noticeUserService.deleteBatchIds(ids);
        } catch (Exception e) {
            return new OaResult(OaResultConstant.FAILED, e.getMessage());
        }
        return new OaResult(OaResultConstant.SUCCESS, null);
    }

}
